package com.sloth.service.impl;

import com.sloth.mapper.CartMapper;
import com.sloth.mapper.CustomerMapper;
import com.sloth.mapper.ProductMapper;
import com.sloth.pojo.Cart;
import com.sloth.pojo.CartExample;
import com.sloth.pojo.Product;
import com.sloth.pojo.ProductAndCartVO;
import com.sloth.service.ShopCarService;
import com.sloth.util.ServerResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class ShopCarServiceImpl implements ShopCarService {

    @Autowired
    private CartMapper cartMapper;

    @Autowired
    private CustomerMapper customerMapper;

    @Autowired
    private ProductMapper productMapper;

    @Override
    public ServerResponse getByCustomerId(Integer customerId) {


        CartExample cartExample = new CartExample();
        cartExample.createCriteria().andCustIdEqualTo(customerId);
        List<Cart> cartList = cartMapper.selectByExample(cartExample);

        List<ProductAndCartVO> productAndCartVOList = new ArrayList<>();
        for (Cart cart : cartList) {
            Product product = productMapper.selectByPrimaryKey(cart.getProId());
            ProductAndCartVO productAndCartVO = new ProductAndCartVO();
            productAndCartVO.setProduct(product);
            productAndCartVO.setCart(cart);
            productAndCartVOList.add(productAndCartVO);
        }
        return ServerResponse.getSuccess(productAndCartVOList);
    }



    @Override
    public ServerResponse delete(Integer cartId) {
        int rows =  cartMapper.deleteByPrimaryKey(cartId) ;
        if(rows >0)
            return ServerResponse.deleteSuccess(rows);
        return ServerResponse.deleteFail(null);
    }




}
